package com.fingard.dsp.bank.directbank.jyrb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/4/24.
 */
public class JYRB01Balance extends JYRB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            JYRB01Header JYRB01Header = new JYRB01Header();
            JYRB01Header.trsCode = "B2EActBalQryHx";
            JYRB01Header.ERPTrsTimestamp = p_req7506.reqHeader.transTime;
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            wirteCommenHeader(xmlTextWriter, JYRB01Header);
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeStartElement("List");
            while (p_req7506.readRecord()) {
                xmlTextWriter.writeStartElement("Map");
                xmlTextWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
                xmlTextWriter.writeEndElement();
            }
            xmlTextWriter.writeEndDocument();
            String[] resultBank = sendToBank(xmlTextWriter.toXmlString(), JYRB01Header.trsCode);

            if (resultBank[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(resultBank[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    if("000000".equals(IBSReturnCode)){
                        while(tmpBkReader.read("/Message/Body/List/Map")){
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            String returnCode =  tmpBkReader.readedXElement.getSingleText("ReturnCode");
                            tmpRetDetail.respCode = "000000".equals(returnCode)?"0":returnCode;
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                            tmpRetDetail.yesterdayBal = tmpBkReader.readedXElement.getSingleText("LastBal");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    }else{
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/head/IBSReturnMsg");
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(IBSReturnMsg);
                    }
                }else{
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            }else{
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(resultBank[1]);
            }
        }catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
